package com.xiaomei.manager.web.controller;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.xiaomei.manager.model.AllowanceTicket;
import com.xiaomei.manager.service.IAllowanceTicketService;
import com.xiaomei.manager.web.dto.req.AllowanceTicketQueryDto;
import com.xiaomei.manager.web.page.Page;

@Controller
@RequestMapping("/allowance")
public class AllowanceTicketController {
	
	@Autowired
	private IAllowanceTicketService allowanceTicketService;
	
	@RequiresPermissions(value="appuser:allowance:view")
	@RequestMapping()
	public String list(AllowanceTicketQueryDto queryDto, Page<AllowanceTicket> page,
			HttpServletRequest request) {
		page.setParamDto(queryDto);
		// 获取代金券列表
		allowanceTicketService.selectByParamsForPage(page);
		request.setAttribute("queryDto", queryDto);
		request.setAttribute("page", page);
		
		return "appuser/allowance/list";
	}
	
    @RequiresPermissions("appuser:allowance:create")
    @RequestMapping(value = "/create", method = RequestMethod.GET)
    public String showCreateForm(String userCode, Model model) {
    	AllowanceTicket ticket = new AllowanceTicket();
    	ticket.setUserCode(userCode);
        model.addAttribute("ticket", ticket);
        model.addAttribute("op", "新增");
        return "appuser/allowance/edit";
    }

    @RequiresPermissions("appuser:allowance:create")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public String create(AllowanceTicket ticket, RedirectAttributes redirectAttributes) {
    	
    	allowanceTicketService.createAllowanceTicket(ticket);
        redirectAttributes.addFlashAttribute("msg", "新增成功");
        return "redirect:/allowance?userCode=" + ticket.getUserCode();
    }
    
    @RequiresPermissions("appuser:allowance:update")
    @RequestMapping(value = "/{id}/update", method = RequestMethod.GET)
    public String showUpdateForm(@PathVariable("id") Integer id, Model model) {
        model.addAttribute("ticket", allowanceTicketService.findOne(id));
        model.addAttribute("op", "修改");
        return "appuser/allowance/edit";
    }

    @RequiresPermissions("appuser:allowance:update")
    @RequestMapping(value = "/{id}/update", method = RequestMethod.POST)
    public String update(AllowanceTicket ticket, RedirectAttributes redirectAttributes){	    		    
    	allowanceTicketService.updateAllowanceTicket(ticket);
    	ticket = allowanceTicketService.findOne(ticket.getId());
        redirectAttributes.addFlashAttribute("msg", "修改成功");
        return "redirect:/allowance?userCode=" + ticket.getUserCode();
    }
    
    /**
     * 删除体验券
     * @param id
     * @param model
     * @return
     */
    @RequiresPermissions("appuser:allowance:delete")
    @RequestMapping(value = "/{id}/delete", method = RequestMethod.GET)
    public String showDeleteForm(@PathVariable("id") Integer id, Model model) {	       
    	model.addAttribute("ticket", allowanceTicketService.findOne(id));
        model.addAttribute("op", "删除");
        return "appuser/allowance/edit";
    }

    @RequiresPermissions("appuser:allowance:delete")
    @RequestMapping(value = "/{id}/delete", method = RequestMethod.POST)
    public String delete(AllowanceTicket ticket, RedirectAttributes redirectAttributes) {	
    	ticket = allowanceTicketService.findOne(ticket.getId());
    	String userCode = ticket.getUserCode();
    	
        allowanceTicketService.deleteAllowanceTicket(ticket.getId());
        redirectAttributes.addFlashAttribute("msg", "删除成功");
        return "redirect:/allowance?userCode=" + userCode;
    }
    
    @InitBinder   
    public void initBinder(WebDataBinder binder) {   
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");   
        dateFormat.setLenient(true);   
        binder.registerCustomEditor(Timestamp.class, new CustomDateEditor(dateFormat, true));   
    }
}
